package com.gitee.wsl.doc.html.readability4j.util

import com.gitee.wsl.ext.CASE_INSENSITIVE
import com.gitee.wsl.ext.compile
import com.gitee.wsl.ext.find
import com.gitee.wsl.ext.matcher


open class RegExUtil {

    companion object {
        const val UnlikelyCandidatesDefaultRegex = "banner|breadcrumbs|combx|comment|community|cover-wrap|disqus|extra|" +
                "foot|header|legends|menu|related|remark|replies|rss|shoutbox|sidebar|skyscraper|social|sponsor|supplemental|" +
                "ad-break|agegate|pagination|pager|popup|yom-remote"

        const val OkMaybeItsACandidateDefaultRegex = "and|article|body|column|main|shadow"

        const val PositiveDefaultRegex = "article|body|content|entry|hentry|h-entry|main|page|pagination|post|text|blog|story"

        const val NegativeDefaultRegex = "hidden|^hid$| hid$| hid |^hid |banner|combx|comment|com-|contact|foot|footer|footnote|" +
                "masthead|media|meta|outbrain|promo|related|scroll|share|shoutbox|sidebar|skyscraper|sponsor|shopping|tags|tool|widget"

        const val ExtraneousDefaultRegex = "print|archive|comment|discuss|e[\\-]?mail|share|reply|all|login|sign|single|utility"

        const val BylineDefaultRegex = "byline|author|dateline|writtenby|p-author"

        const val ReplaceFontsDefaultRegex = "<(/?)font[^>]*>"

        const val NormalizeDefaultRegex = "\\s{2,}"

        const val VideosDefaultRegex = "//(www\\.)?(dailymotion|youtube|youtube-nocookie|player\\.vimeo)\\.com"

        const val NextLinkDefaultRegex = "(next|weiter|continue|>([^\\|]|$)|»([^\\|]|$))"

        const val PrevLinkDefaultRegex = "(prev|earl|old|new|<|«)"

        const val WhitespaceDefaultRegex = "^\\s*$"

        const val HasContentDefaultRegex = "\\S$"
    }


    protected val unlikelyCandidates: Regex

    protected val okMaybeItsACandidate: Regex

    protected val positive: Regex

    protected val negative: Regex

    protected val extraneous: Regex

    protected val byline: Regex

    protected val replaceFonts: Regex

    protected val normalize: Regex

    protected val videos: Regex

    protected val nextLink: Regex

    protected val prevLink: Regex

    protected val whitespace: Regex

    protected val hasContent: Regex


    constructor(unlikelyCandidatesRegex: String = UnlikelyCandidatesDefaultRegex, okMaybeItsACandidateRegex: String = OkMaybeItsACandidateDefaultRegex,
                positiveRegex: String = PositiveDefaultRegex, negativeRegex: String = NegativeDefaultRegex,
                extraneousRegex: String = ExtraneousDefaultRegex, bylineRegex: String = BylineDefaultRegex,
                replaceFontsRegex: String = ReplaceFontsDefaultRegex, normalizeRegex: String = NormalizeDefaultRegex,
                videosRegex: String = VideosDefaultRegex, nextLinkRegex: String = NextLinkDefaultRegex,
                prevLinkRegex: String = PrevLinkDefaultRegex, whitespaceRegex: String = WhitespaceDefaultRegex,
                hasContentRegex: String = HasContentDefaultRegex
    ) {
        this.unlikelyCandidates = Regex.compile(unlikelyCandidatesRegex, Regex.CASE_INSENSITIVE)
        this.okMaybeItsACandidate = Regex.compile(okMaybeItsACandidateRegex, Regex.CASE_INSENSITIVE)
        this.positive = Regex.compile(positiveRegex, Regex.CASE_INSENSITIVE)
        this.negative = Regex.compile(negativeRegex, Regex.CASE_INSENSITIVE)
        this.extraneous = Regex.compile(extraneousRegex, Regex.CASE_INSENSITIVE)
        this.byline = Regex.compile(bylineRegex, Regex.CASE_INSENSITIVE)
        this.replaceFonts = Regex.compile(replaceFontsRegex, Regex.CASE_INSENSITIVE)
        this.normalize = Regex.compile(normalizeRegex)
        this.videos = Regex.compile(videosRegex, Regex.CASE_INSENSITIVE)
        this.nextLink = Regex.compile(nextLinkRegex, Regex.CASE_INSENSITIVE)
        this.prevLink = Regex.compile(prevLinkRegex, Regex.CASE_INSENSITIVE)
        this.whitespace = Regex.compile(whitespaceRegex)
        this.hasContent = Regex.compile(hasContentRegex)
    }


    open fun isPositive(matchString: String): Boolean {
        return positive.matcher(matchString).find()
    }

    open fun isNegative(matchString: String): Boolean {
        return negative.matcher(matchString).find()
    }

    open fun isUnlikelyCandidate(matchString: String): Boolean {
        return unlikelyCandidates.matcher(matchString).find()
    }

    open fun okMaybeItsACandidate(matchString: String): Boolean {
        return okMaybeItsACandidate.matcher(matchString).find()
    }

    open fun isByline(matchString: String): Boolean {
        return byline.matcher(matchString).find()
    }

    open fun hasContent(matchString: String): Boolean {
        return hasContent.matcher(matchString).find()
    }

    open fun isWhitespace(matchString: String): Boolean {
        return whitespace.matcher(matchString).find()
    }

    open fun normalize(text: String): String {
        return normalize.replace(text," ")
    }

    open fun isVideo(matchString: String): Boolean {
        return videos.matcher(matchString).find()
    }

}